package cn.edu.cqu.bysj.wm.gui.component;

import java.awt.BorderLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.ButtonModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingWorker;

import cn.edu.cqu.bysj.wm.def.ConstDef;
import cn.edu.cqu.bysj.wm.def.ConstDef.AlgorithmType;
import cn.edu.cqu.bysj.wm.def.ConstDef.KeyNodeType;
import cn.edu.cqu.bysj.wm.gui.ProgressDialog;
import cn.edu.cqu.bysj.wm.gui.ProgressDialog.SwingWorkExecute;
import cn.edu.cqu.bysj.wm.gui.model.AlgorithmConfig;
import cn.edu.cqu.bysj.wm.gui.model.SQJDAlgorithm;
import cn.edu.cqu.bysj.wm.gui.result.ExecuteAlgorithm;
import cn.edu.cqu.bysj.wm.gui.result.GlobalKeyNodeResult;
import cn.edu.cqu.bysj.wm.gui.result.LocalKeyNodeResult;
import cn.edu.cqu.bysj.wm.gui.table.CommunityKeyNodeView;
import cn.edu.cqu.bysj.wm.gui.table.NodeRankView;

public class JDTotalTabPane extends AlgorithmTabPane {
	public static final String PAGE_RANK = "PageRank";
	public static final String CLUSTER_COEFFICIENT = "Cluster Coefficient";
	public static final String CLOSENESS_CENTRALITY = "Closeness Centrality";
	public static final String STRESS_CENTRALITY = "Stress Centrality";
	public static final String BETWEENNESS_CENTRALITY = "Betweenness Centrality";
	public static final String DEGREE_CENTRALITY = "Degree Centrality";
	JDRightTabContent rightTabContent = new JDRightTabContent(configure);

	/**
	 * create key node total tab
	 * 
	 * @param _title
	 * @param _config
	 */
	public JDTotalTabPane(String _title, AlgorithmConfig _config) {
		super(0.7, _title, _config);
		setLayout(new BorderLayout(0, 0));

		splitPane.setRightComponent(rightTabContent);
		splitPane.setDividerSize(4);
		add(splitPane);

		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		this.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				splitPane.setDividerLocation(0.8);
			}
		});
		if (configure.getAlgorithmType().equals(AlgorithmType.SKYLINE)) {
			rightTabContent.getGlobalParam().setEnabled(false);
			rightTabContent.getLocalParam().setEnabled(false);
		}
	}

	@Override
	public void showResult() {
		// TODO Auto-generated method stub
	}

	public void doAlgorithm() {

		configure.setFlag(rightTabContent.getIndex());
		ButtonModel bm = rightTabContent.getbGroup().getSelection();

		if (bm == null) {
			JOptionPane.showMessageDialog(this, "");
		}
		String cmd = bm.getActionCommand();
		if (cmd.equals(ConstDef.GLOBAL_KEY_NODE_STRING)) {
			try {
				Integer.valueOf((rightTabContent.getGlobalParam().getText()));
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "请输入正确的TopK参数");
				return;
			}
			configure.setKeyNodeType(KeyNodeType.GLOBAL);
			configure.setTopK(Integer.valueOf(rightTabContent.getGlobalParam()
					.getText()));
		} else {
			try {
				Double.valueOf((rightTabContent.getLocalParam().getText()));
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "请输入正确的Percent参数");
				return;
			}
			configure.setKeyNodeType(KeyNodeType.LOCAL);
			configure.setPercent(Double.valueOf(rightTabContent.getLocalParam()
					.getText()));
		}
		Task task = new Task();
		@SuppressWarnings("unused")
		ProgressDialog progressDialog = new ProgressDialog(task);

	}

	private void showResult(LocalKeyNodeTabContent keycontent) {
		CommunityKeyNodeView cView = new CommunityKeyNodeView(
				ConstDef.LOCAL_KEY_NODE_STRING + configure.getTitle(), 20);
		LocalKeyNodeResult temp = (LocalKeyNodeResult) result;
		cView.initDatas(temp.getCommunityKeyNodeRows());
		keycontent.add(new JScrollPane(cView));
		splitPane.setDividerLocation(0.8);
	}

	private void showResult(GlobalKeyNodeTabContent gkconten) {
		int[] flags = configure.getFlag();
		int sum = 0;
		for (int i = 0; i < flags.length; i++) {
			sum += flags[i];
		}
		String[] heads = new String[sum];
		@SuppressWarnings("rawtypes")
		Class[] classes = new Class[sum];
		boolean[] booleans = new boolean[sum];
		for (int i = 0, j = 0; j < flags.length; j++) {
			if (flags[j] != 0) {
				classes[i] = Double.class;
				booleans[i] = false;
				switch (j) {
				case 0:
					heads[i] = DEGREE_CENTRALITY;
					break;
				case 1:
					heads[i] = BETWEENNESS_CENTRALITY;
					break;
				case 2:
					heads[i] = STRESS_CENTRALITY;
					break;
				case 3:
					heads[i] = CLOSENESS_CENTRALITY;
					break;
				case 4:
					heads[i] = CLUSTER_COEFFICIENT;
					break;
				case 5:
					heads[i] = PAGE_RANK;
					break;
				}
				i++;
			}
		}
		NodeRankView nView = new NodeRankView(ConstDef.GLOBAL_KEY_NODE_STRING
				+ configure.getTitle(), heads, classes, booleans, 10);
		GlobalKeyNodeResult temp = (GlobalKeyNodeResult) result;
		nView.initDatas(temp.getNodeRankRows());
		gkconten.getPanel().add(new JScrollPane(nView));

		gkconten.getKeyNodeSet().setText(temp.getKeyNodes().toString());
		gkconten.getSpearman().setText(temp.getSpearman());
		splitPane.setDividerLocation(0.8);

	}

	public class Task extends SwingWorker<Void, Void> implements
			SwingWorkExecute {
		private ProgressDialog progressDialog;

		/*
		 * main task, executed in background thread
		 */
		@Override
		public Void doInBackground() {
			getParent().setEnabled(false);
			SQJDAlgorithm executer = new ExecuteAlgorithm();
			result = executer.execute(configure);

			return null;
		}

		/*
		 * executed in event dispatch thread
		 */
		public void done() {
			progressDialog.dispose();
			if (result instanceof GlobalKeyNodeResult) {
				GlobalKeyNodeTabContent gkconten = new GlobalKeyNodeTabContent();
				splitPane.setLeftComponent(gkconten);
				showResult(gkconten);
			} else {
				LocalKeyNodeTabContent keycontent = new LocalKeyNodeTabContent();
				splitPane.setLeftComponent(keycontent);
				showResult(keycontent);
			}
			getParent().setEnabled(true);

		}

		public void execute(ProgressDialog progressDialog) {
			super.execute();
			this.progressDialog = progressDialog;
		}
	}
}
